#include <iostream>
#include <cstdlib>
#include <string>
#include <unistd.h>
using namespace std;
bool GetQueryString(std::string &query_string)
{
    std::string Method = getenv("METHOD");
    std::cerr << "Debug Method: " << Method << std::endl;
    bool result;
    if (Method == "GET")
    {
        query_string = getenv("QUERY_STRING");
        // std::cerr<<"Get Method Debug Query_String: "<<query_string<<std::endl;
        result = true;
    }
    else if (Method == "POST")
    {
        std::cerr << "Content-Length: " << getenv("CONTENT_LENGTH") << std::endl;
        int content_length = atoi(getenv("CONTENT_LENGTH"));
        char c = 0;
        while (content_length--)
        {
            int a = read(0, &c, 1);
            if (a <= 0)
            {
                break;
            }
            query_string.push_back(c);
        }
        // std::cerr<<"Post Method Debug Query_String: "<<query_string<<std::endl;
        result = true;
    }
    else
    {
        result = false;
    }
    return result;
}

void CutString(std::string &in, const std::string &sep, std::string &out1, std::string &out2)
{
    auto pos = in.find(sep);
    if (pos != std::string::npos)
    {
        out1 = in.substr(0, pos);
        out2 = in.substr(pos + sep.size());
    }
}
int main()
{ // ← 一定要是全局作用域的 int main()

    std::string query_string;
    bool res = GetQueryString(query_string);
    if (res)
    {
        // GET /index.cgi?a=100&b=200 HTTP/1.1
        // POST /index.cgi HTTP/1.1

        std::string out1;
        std::string out2;
        CutString(query_string, "&", out1, out2);

        std::string key1;
        std::string value1;
        CutString(out1, "=", key1, value1);

        std::string key2;
        std::string value2;
        CutString(out2, "=", key2, value2);

        // 往1打印结果 也就是管道
        std::cout << key1 << "=" << value1 << std::endl;
        std::cout << key2 << "=" << value2 << std::endl;

        // 往2打印结果 也就是本地终端
        std::cerr << key1 << "=" << value1 << std::endl;
        std::cerr << key2 << "=" << value2 << std::endl;

        int x = atoi(value1.c_str());
        int y = atoi(value2.c_str());

        cout << "Content-Type: text/html\r\n\r\n";
        cout << "<!DOCTYPE html><html lang='zh-CN'>\n";
        cout << "<head><meta charset='utf-8'>"
                "<style>"
                "body{font-family:Arial;background:#f5f7fa;padding:40px;}"
                "h2{color:#333;}"
                "table{border-collapse:collapse;margin-top:20px;}"
                "td,th{border:1px solid #aaa;padding:10px 20px;text-align:center;}"
                "th{background:#66a6ff;color:#fff;}"
                "</style></head>\n";
        cout << "<body>";
        cout << "<h2>计算结果 (a=" << x << ", b=" << y << ")</h2>\n";
        cout << "<table>\n";
        cout << "<tr><th>运算</th><th>表达式</th><th>结果</th></tr>\n";
        cout << "<tr><td>加法</td><td>" << x << " + " << y << "</td><td>" << (x + y) << "</td></tr>\n";
        cout << "<tr><td>减法</td><td>" << x << " - " << y << "</td><td>" << (x - y) << "</td></tr>\n";
        cout << "<tr><td>乘法</td><td>" << x << " * " << y << "</td><td>" << (x * y) << "</td></tr>\n";
        if (y != 0)
            cout << "<tr><td>除法</td><td>" << x << " / " << y << "</td><td>" << (x / y) << "</td></tr>\n";
        else
            cout << "<tr><td>除法</td><td>" << x << " / " << y << "</td><td>除数为0</td></tr>\n";
        cout << "</table>\n";
        cout << "</body></html>\n";
    }
    return 0;
}
